package com.zx.sys.cms.api.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.R;
import com.zx.sys.cms.entity.CmsChannelEntity;
import com.zx.sys.cms.entity.CmsContentEntity;
import com.zx.sys.cms.service.CmsChannelService;
import com.zx.sys.cms.service.CmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/front/cms")
public class CmsApi {
    @Autowired
    private CmsChannelService cmsChannelService;
    @Autowired
    private CmsContentService cmsContentService;

    @RequestMapping("/channels")
    public R getAllList(HttpServletRequest request){
        QueryWrapper<CmsChannelEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_display",1);
        queryWrapper.orderByAsc("priority");
        List<CmsChannelEntity> list = cmsChannelService.list(queryWrapper);
        return R.ok().put("info", list);
    }

    @RequestMapping("/contents")
    public R getContentsByChannel(@RequestParam Map<String, Object> params){
        PageUtils page = cmsContentService.queryPage(params);
        System.out.println("channel.id" + params.get("channelid"));
        return R.ok().put("page", page);
    }

    @RequestMapping("/content/{id}")
    public R content(@PathVariable("id") Long id){
        CmsContentEntity cmsContent = cmsContentService.getById(id);
        return R.ok().put("info", cmsContent);
    }

    @RequestMapping("/channel/{id}")
    public R channel(@PathVariable("id") Long id) {
        CmsChannelEntity cmsChannel = cmsChannelService.getById(id);
        return R.ok().put("info", cmsChannel);
    }
}
